﻿using System;
using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.SaleCategorys;
using Ue.Qcdz.Domain.Shops;
namespace Ue.Qcdz.Repositories.SaleCategorys
{
    public class SateCategryRepository : SqlServerRepository<SaleCategory>, ISaleCategoryRepository
    {
        public SateCategryRepository()
            : this(null) { }

        public SateCategryRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork) { }

        protected override System.Data.IDataParameter[] BuildParameters(SaleCategory entity)
        {
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.Name,entity.Name??""),              
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.Code,entity.Code??""),   
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.Status,entity.Status),              
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.ShopId,entity.Shop.Key),
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.DisplayOrder,entity.DisplayOrder),
              this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.CreateOn,entity.CreateOn)
  
            };
        }

        protected override IWhereClause BuildWhereClauseBy(Infrastructure.DomainBase.IDataQuery dataQuery)
        {
            if (dataQuery is ISaleCategoryDataQuery)
            {
                return new SaleCategryDataQueryWhereClause(dataQuery as ISaleCategoryDataQuery);
            }

            return null;
        }

        protected override string[] GetBaseInsertFields()
        {

            return new string[] {
                    SaleCategryFactory.FieldNames.EntityKey,
                    SaleCategryFactory.FieldNames.Name,
                    SaleCategryFactory.FieldNames.Code, 
                    SaleCategryFactory.FieldNames.ShopId,
                    SaleCategryFactory.FieldNames.Remark,
                    SaleCategryFactory.FieldNames.Status, 
                    SaleCategryFactory.FieldNames.DisplayOrder,
                    SaleCategryFactory.FieldNames.CreateOn
     
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
                    SaleCategryFactory.FieldNames.EntityKey,
                    SaleCategryFactory.FieldNames.Name,
                    SaleCategryFactory.FieldNames.Code, 
                    SaleCategryFactory.FieldNames.Remark,
                    SaleCategryFactory.FieldNames.Status, 
                    SaleCategryFactory.FieldNames.DisplayOrder
            };
        }

        protected override IDictionary<string, SqlServerRepositoryBase<SaleCategory>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<SaleCategory>.ChildDataHandler>();
            callBacks.Add(SaleCategryFactory.FieldNames.ShopId, delegate(SaleCategory saleCategory, object fieldValue)
            {
                SaleCategoryFroxy saleCategoryProxy = saleCategory as SaleCategoryFroxy;
                if (saleCategoryProxy != null)
                {
                    IShopRepository shopRepository = RepositoryFactory.GetRepository<IShopRepository, Shop>();
                    saleCategoryProxy.Shop = shopRepository[fieldValue];
                }
            });
            return callBacks;
        }

        protected override string GetDataTableName()
        {
            return "SaleCategory";
        }
        protected override string GetOrderBy()
        {
            return "CreateOn DESC";
        }

        protected override string GetKeyFieldName()
        {
            return "id";
        }

        #region Implementation of ISaleCategoryRepository

        public IList<SaleCategory> FindBy(string shopId, int status)
        {
            var query = "SELECT * FROM SaleCategory WHERE ShopId=@ShopId AND Status=@Status";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.ShopId,shopId),
                                    this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.Status,status)
                                };
            return this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion



        #region ISaleCategoryRepository 成员


        public void RemoveAll(Shop shop)
        {
            if (shop == null) return;
            var query = "DELETE FROM SaleCategory WHERE ShopId=@ShopId";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(SaleCategryFactory.FieldNames.ShopId,shop.Key),
                             
                                };
            this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
